package yzrilyzr.simplesynth.app.fragment;
import static android.content.Context.MODE_PRIVATE;

import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import yzrilyzr.simplesynth.app.R;
import yzrilyzr.simplesynth.app.view.PianoKeyboardView;

public class PianoKeyboardFragment extends Fragment implements View.OnTouchListener {
	public PianoKeyboardView kb1, kb2;
	SharedPreferences sp;
	private Handler handler = new Handler();
	private Runnable repeatAction;
	private int currentActionId;
	private boolean isPressed = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.pianokeyboard, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		kb1 = view.findViewById(R.id.pianokeyboard1);
		kb2 = view.findViewById(R.id.pianokeyboard2);
		sp = view.getContext().getSharedPreferences("PianoKeyboardConfig", MODE_PRIVATE);
		kb1.setBorderColor(0xFF2257FF);
		kb2.setBorderColor(0xFFFF5722);
		kb1.setStartKeyOffset(sp.getInt("StartKeyOffset1", 21));
		kb2.setStartKeyOffset(sp.getInt("StartKeyOffset2", 28));
		kb1.setWhiteKeyCount(sp.getInt("WhiteKeyCount1", 8));
		kb2.setWhiteKeyCount(sp.getInt("WhiteKeyCount2", 8));

		// 设置触摸监听器
		view.findViewById(R.id.keyboard1Left).setOnTouchListener(this);
		view.findViewById(R.id.keyboard1Right).setOnTouchListener(this);
		view.findViewById(R.id.keyboard1Add).setOnTouchListener(this);
		view.findViewById(R.id.keyboard1Remove).setOnTouchListener(this);
		view.findViewById(R.id.keyboard2Left).setOnTouchListener(this);
		view.findViewById(R.id.keyboard2Right).setOnTouchListener(this);
		view.findViewById(R.id.keyboard2Add).setOnTouchListener(this);
		view.findViewById(R.id.keyboard2Remove).setOnTouchListener(this);

		// 初始化重复动作
		repeatAction = new Runnable() {
			@Override
			public void run() {
				if (isPressed) {
					performAction(currentActionId);
					handler.postDelayed(this, 200); // 每0.2秒执行一次
				}
			}
		};
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int id = v.getId();
		switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				isPressed = true;
				currentActionId = id;
				performAction(id); // 立即执行一次
				handler.postDelayed(repeatAction, 200); // 开始重复执行
				return true;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
				isPressed = false;
				handler.removeCallbacks(repeatAction);
				saveConfig(); // 保存配置
				return true;
		}
		return false;
	}

	private void performAction(int id) {
		if (id == R.id.keyboard1Left) kb1.moveLeft();
		else if (id == R.id.keyboard1Right) kb1.moveRight();
		else if (id == R.id.keyboard1Add) kb1.increaseKeys();
		else if (id == R.id.keyboard1Remove) kb1.decreaseKeys();
		else if (id == R.id.keyboard2Left) kb2.moveLeft();
		else if (id == R.id.keyboard2Right) kb2.moveRight();
		else if (id == R.id.keyboard2Add) kb2.increaseKeys();
		else if (id == R.id.keyboard2Remove) kb2.decreaseKeys();
	}

	private void saveConfig() {
		SharedPreferences.Editor editor = sp.edit();
		editor.putInt("StartKeyOffset1", kb1.getStartKeyOffset());
		editor.putInt("StartKeyOffset2", kb2.getStartKeyOffset());
		editor.putInt("WhiteKeyCount1", kb1.getWhiteKeyCount());
		editor.putInt("WhiteKeyCount2", kb2.getWhiteKeyCount());
		editor.apply();
	}
}